/**
 * 
 */
package com.gms.server.service;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONWriter;

import com.gms.server.model.User;
import com.gms.tool.ShareData;
import com.gms.tool.mysql.MysqlConnection;

/**
 * @author Julien
 *
 */
public class CreateUser extends HttpServlet {

	private static final long serialVersionUID = 5352597685915190315L;
	
	private static final String USER_CREATED = "userCreated";
	private static final String YES = "yes";
	private static final String NO = "no";

	private static final int NO_USER = 0;
	private static final int ALREADY_USER = 1;
	private static final int ALREADY_MAIL = 2;
	private static final int CREATION_ERROR = 3;

	private static final String ERROR_CREATION_CODE = "errorCode";


	
	
	public CreateUser() {
		super();
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String username = request.getParameter("name");
		String mail = request.getParameter("mail");
		String password = request.getParameter("password");
		
		User user = new User(username, mail, password);

		JSONWriter writer = new JSONWriter(response.getWriter());
		
		try {
			
			int alreadyExist = checkUserAlreadyExist(user);
			System.out.println("alreadyExist : " + alreadyExist);
			
			switch (alreadyExist) {
			case NO_USER:
				boolean isCreated = MysqlConnection.createUser(username, password, mail);
				System.out.println("isCreated : " + isCreated);
				
				writer.object();
				if (isCreated) {
					writer.key(USER_CREATED).value(YES);
				} else {
					System.out.println("CREATION_ERROR");
					writer.key(USER_CREATED).value(NO);
					writer.key(ERROR_CREATION_CODE).value(CREATION_ERROR);
				}
				writer.endObject();
				break;
			case ALREADY_USER:
				System.out.println("ALREADY_USER");
				writer.object();
				writer.key(USER_CREATED).value(NO);
				writer.key(ERROR_CREATION_CODE).value(ALREADY_USER);
				writer.endObject();
				break;
			case ALREADY_MAIL:
				System.out.println("ALREADY_MAIL");
				writer.object();
				writer.key(USER_CREATED).value(NO);
				writer.key(ERROR_CREATION_CODE).value(ALREADY_MAIL);
				writer.endObject();
				break;

			default:
				System.out.println("default");
				writer.object();
				writer.key(USER_CREATED).value(NO);
				writer.key(ERROR_CREATION_CODE).value(CREATION_ERROR);
				writer.endObject();
				break;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private int checkUserAlreadyExist(User user) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		System.out.println("checkUserAlreadyExist : "+user.getUsername()+"/"+user.getMail());
		// The table name is : usertable and it has two columm : username and userpassword
		ResultSet resultSet;
		
		resultSet = MysqlConnection.selectUser(user.getUsername());
		while (resultSet.next()) {
			System.out.println("il y a des noms");
			if (resultSet.getString(ShareData.USERS_NAME) != null) {
				return ALREADY_USER;
			}
		}
		resultSet = MysqlConnection.selectmail(user.getMail());
		while (resultSet.next()) {
			System.out.println("Il y a des mails");
			if (resultSet.getString(ShareData.USERS_MAIL) != null) {
				return ALREADY_MAIL;
			}
		}
		return NO_USER;
	}
}
